//--------------------------------------------------------------------------------
// This file is a portion of the D2D1Renderer.  It is distributed
// under the MIT License, available in the root of this distribution and 
// at the following URL:
//
// http://www.opensource.org/licenses/mit-license.php
//
// Copyright (c) Mirco Timmermann
//
//--------------------------------------------------------------------------------
#include "Button.h"

namespace D2 {

Button::Button(int x, int y, int width, int height) : DisplayObject(x, y, width, height) {
	_pressed = false;
	_highlight = false;

	_toggle = false;

	_text = new Text();
}

Button::~Button(void) {

}

void Button::SetText(std::string text) {
	_text->SetText(text);
}

std::string Button::GetText() {
	return _text->GetText();
}

/////////////////////////////DATA////////////////////////////
bool Button::Draw(Direct2DContext* ctx, D2D1::Matrix3x2F* mtx) {
	ctx->lineWidth = 1;
	ctx->SetStrokeStyle(0.3f, 0.3f, 0.3f);
	ctx->SetFillStyle(0.5f, 0.5f, 0.5f);

    ctx->rect(0, 0, _width, _height);
    ctx->fill();
	ctx->stroke();

	if(_pressed) {
		ctx->SetFillStyle(0.8f, 0.8f, 0.8f);
		ctx->rect(0, 0, _width, _height);
        ctx->fill();
	} else if(_highlight) {
		ctx->SetFillStyle(0.6f, 0.6f, 0.6f);
		ctx->rect(0, 0, _width, _height);
		ctx->fill();
	}

	if(_text->GetText() != "") {
		_text->Draw(ctx);
	}

	return true;
}

void Button::OnMouseOver(const MouseEvent &e) {
	_highlight = true;
}

void Button::OnMouseOut(const MouseEvent &e) {
	_highlight = false;
}

void Button::OnMouseDown0(const MouseEvent &e) {
	_pressed = true;
}

void Button::OnMouseUp0(const MouseEvent &e) {
	_pressed = false;
    _toggle = !_toggle;
}

void Button::OnMouseLeave0(const MouseEvent &e) {
	_pressed = false;
}

}